{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "30c2dbc4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.01587624 0.11731043 0.86681333]\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from numpy import random\n",
    "\n",
    "# 模拟实现softmax函数\n",
    "def softmax(array):\n",
    "    \"\"\"\n",
    "    t 对每一个元素取幂\n",
    "    s 幂的相加求和\n",
    "    result 元素除以s得到对应概率的数组\n",
    "\n",
    "    :param array: 输入数组\n",
    "    :return: 返回概率\n",
    "    \"\"\"\n",
    "    t = np.exp(array) # 对每一个元素取幂\n",
    "    s = sum(t) # 幂的相加求和\n",
    "    result = t / s # 元素除以s得到对应概率的数组\n",
    "    return result\n",
    "\n",
    "# 定义数组\n",
    "a = np.array([3, 5, 7])\n",
    "# a = random.randint(0, 3, 256)\n",
    "result = softmax(a)\n",
    "print(result)\n",
    "print(sum(result))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "931acdd8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "main",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
